<?php
/**
 * Created by xiaosong
 * E-mail:306027376@qq.com
 * Date: 2018/11/12
 * Time: 17:37
 */

namespace app\api\controller;


use app\common\logic\Logic;
use think\Db;
use think\Exception;
use think\Log;
use Yansongda\Pay\Pay;

class Pays extends User
{

    protected static $alipay = [

        'app_id' => '2019052965380752',
//        'app_id' => '2019072565931723',
        'notify_url' => 'http://api.haniyuyin.com/pays/notify',
//        'ali_public_key'   => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiF4Y1bF3uVl6+xdUE+HqeG6HkiRsmnvnszkqv4v9iHrQJHpp7r9GXd1QO51MKjvU3OBOI6I/RLxADtaL8RmQ4AIHYdye5GreITFivbXIMtTprG0AF8gCw8tjWTD+iwuYgbXDh+OniKR3Dks2IEDiwOHilnNGCbib0CIwXDodPxq/avOMMguVxHfsVVIVvzxmaFKun9h8IVC4JGuXaLgV0AHrmqgk7zsTdMQ1pn9AGAh/tW4MC+wb4VPSuDCexU+GSktZZEQU5DBk/mTR+9VuLWDLyEU34sspvlb3TnmDMvPsnA+WxSXSGQeyyk+T2u1tz5/YLTnJ4KzusKwUz+MPSQIDAQAB',
//        'ali_public_key'   => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnZrKxbHgYhvT8bUnQXZKKsYKhMKFRSBWRiykrQsiUL5duOaG/sieHG65GOtcqzIsPWH2EMO9UkTekEKoepQAL1orbd3IEOd2aJyc057jMuqZ+KQ10hxJg6LBqQMDbz14QmI3N93SmtrIL1HdavazuU3siXJdYCKLkPqJREPovTXBoI3rTOyQ+gmbDWS6itroySsXugWIjMlHk0WJZdSct+ahQJt6nU+7H07QI+OFYolzwx5FXmQRGNlfVfc5YOHrnNFokoYkm+mj/hcMNsfUHCFuyzgXCH5yjO1zAFbfg2nmrEhd7xEHchbTbDisGxyzE415mVLQbrWzp6paSA8oTQIDAQAB',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AM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TNUSyojQsoAkn1Y1yhWIfSCBn/wh+369bTMtOH+lNH0QKBgQDXE6EwnUE21jzahXNXAWFmMvYGQS3dGiMYKjnBE1+th4Kjl/8xBYGXVWLM3krGqa9bF/ak2X7eCJ8qZ7AV5P7pR4VhNEOHe2ZAkyauz3MTP0GvleN2Z4nXKMGBXx+PapJMVqm+ojqq+Trzrbf11TKgSnmwpQtNFdtdN7uGcXNCLwKBgQCmQLEJyzkSiACR1diMYhvU/tqA+ZU8Nk2+OEvjbcsChRBJlmWCaR3dlNSSy7jDuvtKq2PoHm+6D+c+o1AlmH0xe4H/w+1hoMTyuEaa+2p1R9xXeCsrO8u5U6yhlD6tsXg9gzLDKS7l/2oReU+2Lv92sGwtdPt8AdSuHxecYOGX5wKBgQDV/LTSfpNk8PDo5f/k3/29RhTcEjuEkb27DEAcJp96Mqo7BD6l0nmmTREZDk5NrIRI2OXQDImade+OrE1Y61boWP2OwgYyJQeOVhoVsVcoHjbHcRU4ODm/tdAZXMA02+6lP1Bx2E0Q0RnjWsL+N1nAe3jp1LwSvR1deNb+I6BuhwKBgCRuPp0vzCTUapOKcbZpdUHaStVCW8Ji/+wz9+UxVPALaoNFkp7eG+5HQ8GbJLS3T4OQRQdDTCZQf9LJBLIIVT63msgKDgV7rkyltssVVsG2aoE8UvFZXA4TWNvbWVTEc92+1yvO5Xj+3CZSF/aLG+3NQRdA8C6/U/SH2aupyyV3AoGBAJeSLzDNty56lnpeinyAy/hkq+sMqJgs9UJ9IgkNhJD6qu3/yRp7Qms2/bBYn0PPR0ojW7lwgia4k7QIJ1tOPem4LOapShsn3yzP/U/VIKWXliL9VlI+qYrjEC2Q743NfIEFpIZ8cXt//hnP2iQ1bDVE0+z9WswR3eT29z+jkJ2w'
        ];

    protected static $wechat = [
        'appid' => 'wxaf912050f08213b7', // APP APPID
        'app_id' => 'wxaf912050f08213b7', // APP APPID
//        'mch_id' => '1521927091',
        'mch_id' => '1537399441',
        'key' => 'e10a123949ba59abbe56e057f20f883e',
        'notify_url' => 'http://api.haniyuyin.com/pays/wechat',
//        'cert_client' => './apiclient_cert.pem', // optional，退款等情况时用到
//        'cert_key' => './apiclient_key.pem',// optional，退款等情况时用到
    ];




    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 支付签名获取
     */
    public function paySign()
    {
        $this->ApiLimit(1,$this->user_id);

        $data = input('post.');

        $order_num = 'YW'.hashid($this->user_id).time().rand(1000,9999);

        switch ($data['type']){
            case 'recharge': //钻石充值

                if ($data['useCard'] == 1){
                    //查询是否有充值加成卡
                    $map1['user_id'] = $this->user_id;
                    $map1['type']    = 2;
                    $map1['key']     = 3;

                    $num = Db::name('my_package')->where($map1)->value('num')??0;

                    if ($num == 0){
                        api_return(0,'暂无充值加成卡');
                    }
                }else{
                    $data['useCard'] = 0;
                }

                $map['r_id']   = $data['id'];
                $map['status'] = 1;
                $config = Db::name('recharge_config')->where($map)->find();
                if (!$config || $config['price'] <= 0) api_return(0,'参数错误');

                $price = $config['price'];

                $subject = '哈尼'.$config['money'].'钻充值';

                $body = json_encode([
                    'user_id' => hashid($this->user_id),
                    'id'=>$data['id'],
                    'type' => $data['type'],
                    'remark' => $subject,
                    'useCard' => $data['useCard']
                ]);

                break;
            case 'gift': //礼物赠送
                $array   = $this->giveGift($data,$this->user_id);
                $price   = $array['price'];
                $subject = $array['subject'];
                unset($array['subject']);

                $body = json_encode($array);
                break;
            case 'invite': //邀约订单付款

                $map['order_id']   = $data['order_id'];
                $map['status'] = 0;
                $order = Db::name('order')->where($map)->find();

                if (!$order) api_return(0,'订单号错误');

                $price = $order['price'];

                $subject = '哈尼邀约';

                $body = json_encode([
                    'user_id' => hashid($this->user_id),
                    'order_id'=>$data['order_id'],
                    'type' => $data['type']
                ]);

                break;
            case 'noble': //贵族购买付款

                $noblePrice = $this->noblePrice($this->user_id);

                $price = $noblePrice['price'];

                $subject = '哈尼贵族购买';
                $noble['user_id']    = hashid($this->user_id);
                $noble['type']       = $data['type'];
                $noble['price']      = $noblePrice['price'];
                $noble['give']       = $noblePrice['give'];
                $noble['noble_id']   = $noblePrice['noble_id'];
                $noble['noble_time'] = $noblePrice['noble_time'];
                $noble['noble_name'] = $noblePrice['noble_name'];
                $body = json_encode($noble);

                break;

            case 'guard': //守护购买付款

                $price = $this->guard_price($data['guard_type']);
                $data['user_id'] = $this->user_id;
                $this->checkGuard($data);

                if (!is_numeric($price) || $price < 1){
                    api_return(0,'参数错误');
                }

                $subject = '守护付费';
                $guard['user_id']       = hashid($this->user_id);
                $guard['type']          = $data['type'];
                $guard['guard_type']    = $data['guard_type'];
                $guard['guard_user']    = $data['guard_user'];
                $body = json_encode($guard);

                break;

            default:
                api_return(0,'类型错误');
                break;

        }

        $record['content']     = $body;
        $record['create_time'] = time();
        $record['update_time'] = time();
        $id = Db::name('paysign_record')->insertGetId($record);
        if (!$id) api_return(0,'记录错误');
        $subject .= sprintf( '(id:%s)',$this->userInfo('uuid'));
        switch ($data['payType']){

            case 'alipay'://支付宝支付
                $order = [
                    'out_trade_no' => $order_num,
//                    'total_amount' => '0.01', //单位元
                    'total_amount' => $price, //单位元
                    'subject' => $subject,
//                    'body' =>  $body,//自定义参数
                    'body' =>  $id,//自定义参数

                ];
                try{
                    $sign = Pay::alipay(self::$alipay)->app($order)->getContent();

                }catch (\Exception $e){
                    api_return(10,'支付宝已关闭!');
                }
                break;
            case 'wechat': //微信支付
                $order = [
                    'out_trade_no' => $order_num,
                    'body' => $subject,
//                    'total_fee' => '1',
                    'total_fee' => $price*100, //单位 分
//                    'attach'=> $body,
                    'attach'=> $id,
                ];

                $json = Pay::wechat(self::$wechat)->app($order)->getContent();
                $sign = json_decode($json,true);

                break;

            default:
                api_return(0,'支付类型错误');
                break;

        }
//        print_r($sign);exit;
        api_return(1,'获取成功',$sign);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 支付宝回调
     */
    public function notify()
    {

        $alipay = Pay::alipay(self::$alipay);
        Db::startTrans();
        try{
            $data = $alipay->verify(); // 是，验签就这么简单！
//            $data = cache('aaatest');
//            dd($data)
//                ;
            $map['out_trade_no'] = $data['trade_no'];
            $map['trade_no']     = $data['out_trade_no'];
            $map['pay_type']     = 1;
            $have = Db::name('pay_order')->where($map)->find();
            if ($have){ //订单已处理
                return $alipay->success()->send();
            }

            if ($data['trade_status'] == 'TRADE_SUCCESS' || $data['trade_status'] == 'TRADE_FINISHED'){

                $record = Db::name('paysign_record')->where('id',$data['body'])->value('content');
                if (!$record){
                    Log::record('查询不到支付签名记录');
                }
                $body = json_decode($record,true);

                switch ($body['type']){
                    case 'recharge'://充值
                        $body['money']  = $data['total_amount'];
                        $body['remark'] = $data['subject'];
                        $this->recharge($body,2);
                        break;

                    case 'gift': //礼物赠送

                        $this->gift($body);
                        $this->addLevel($body['total_amount'],dehashid($body['user_id']));
                        break;

                    case 'invite': //邀约订单付款
                        $order['out_trade_no'] = $data['trade_no'];
                        $order['trade_no']     = $data['out_trade_no'];
                        $order['out_price']    = $data['total_amount'];
                        $order['pay_way']      = 2;
                        $order['status']       = 1;
                        if ($this->invite($body,$order)){
                            $this->addLevel($data['total_amount'],dehashid($body['user_id']));
                        }
                        break;
                    case 'noble':
                        if ($this->noble($body)){

                            $this->addLevel($data['price'],dehashid($body['user_id']));
                        }

                        break;
                    case 'guard':
                        $body['user_id'] = dehashid($body['user_id']);
                        $body['guard_user'] = dehashid($body['guard_user']);
                        $body['type'] = $body['guard_type'];
                        $this->guardOpen($body,1);
                        break;

                    default:
                        api_return(0,'类型错误');
                        break;
                }

            }

            Db::commit();

//            Log::debug('Alipay notify', $data->all());
        } catch (Exception $e) {
            Db::rollback();
            cache('message',$e->getMessage());exit();
//             exit('error');
        }
        $item['out_trade_no'] = $data['trade_no'];
        $item['trade_no']     = $data['out_trade_no'];
        $item['type']         = $body['type'];
        $item['price']        = $data['total_amount'];
        $item['user_id']      = dehashid($body['user_id']);
        $item['pay_type']     = 1;
        $item['create_time']  = time();
        $item['update_time']  = time();
        Db::name('pay_order')->insert($item);
        return $alipay->success()->send();
    }


    public function wechat()
    {
//        cache('wechatData',input());
//        $data = cache('wechatData');
//        dd($data);
        $pay = Pay::wechat(self::$wechat);

        try{
            $verify = $pay->verify(); // 是的，验签就这么简单！
            cache('wechatVerify');
            $map['out_trade_no'] = $verify['transaction_id'];
            $map['trade_no']     = $verify['out_trade_no'];
            $map['pay_type']     = 2;
            $have = Db::name('pay_order')->where($map)->find();
            if ($have){ //订单已处理
                return $pay->success()->send();
            }
//            cache('verify',$verify);
//            $verify = cache('wechat');
            $record = Db::name('paysign_record')->where('id',$verify['attach'])->value('content');
            if (!$record){
                Log::record('查询不到支付签名记录');
            }
            $body = json_decode($record,true);
//            $body = json_decode($verify['attach'],true);
            $total = $verify['cash_fee'] / 100;


            switch ($body['type']){
                case 'recharge'://充值
                    $body['money']  = $total;
                    $this->recharge($body,1);
                    break;

                case 'gift': //礼物赠送

                    $this->gift($body);
                    $this->addLevel($total,dehashid($body['user_id']));
                    break;

                case 'invite': //邀约订单付款
                    $order['out_trade_no'] = $verify['transaction_id'];
                    $order['trade_no']     = $verify['out_trade_no'];
                    $order['out_price']    = $total;
                    $order['pay_way']      = 3;
                    $order['status']       = 1;
                    if ($this->invite($body,$order)){
                        $this->addLevel($total,dehashid($body['user_id']));
                    }
                    break;
                case 'noble':

                    if ($this->noble($body)){
                        $this->addLevel($body['price'],dehashid($body['user_id']));
                    }

                    break;
                case 'guard':
                    $body['user_id'] = dehashid($body['user_id']);
                    $body['guard_user'] = dehashid($body['guard_user']);
                    $body['type'] = $body['guard_type'];
                    $this->guardOpen($body,1);
                    break;
                default:
                    api_return(0,'类型错误');
                    break;
            }
        } catch (Exception $e) {
            // $e->getMessage();
            cache('wechatMsg',$e->getMessage());exit();
        }

        $item['out_trade_no'] = $verify['transaction_id'];
        $item['trade_no']     = $verify['out_trade_no'];
        $item['type']         = $body['type'];
        $item['user_id']      = dehashid($body['user_id']);
        $item['price']        = $total;
        $item['pay_type']     = 2;
        $item['create_time']  = time();
        $item['update_time']  = time();
        Db::name('pay_order')->insert($item);
        return $pay->success()->send();// laravel 框架中请直接 `return $pay->success()`
    }







    public function test()
    {
        $data = cache('aliTest');

        dd($data);
    }





    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 邀约修改
     */
    protected function invite($data,$order)
    {

        $model = new Logic();
        $model->changeTable('order');
        $map['status'] = 0;
        $map['order_id'] = $data['order_id'];

        $item = $model->where($map)->find();

        if (!$item || $item['status'] != 0){
            return false;
        }
        $online = $this->userExtra('online_status',$item['to_user']);
        if ($online != 1){
            $form_name = Db::name('skill_form')->where('form_id',$item['form_id'])->value('form_name');
            $skill = Db::name('skill')->where('skill_id',$item['skill_id'])->find();
            $phone = $this->userInfo('phone',$item['to_user']);
            $val['val1'] = substr($phone,-4,4);
            $val['val2'] = date('m月d日 H:i').'的'.$skill['skill_name']."($form_name)";
            //发送短信
            sendCustomSms($this->userInfo('phone',$item['to_user']),$val,'order');
        }
        return $item->save($order);

    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 贵族购买
     */
    protected function noble($data)
    {

        $price = $data['price'];
        $give  = $data['give'];
        if ($price <= 0) api_return(0,'价格错误');

        $dehash = dehashid($data['user_id']);

        //加上赠送金额
        Db::name('users')->where('user_id',$dehash)->setInc('money',$give);

        $update['noble_id']   = $data['noble_id'];
        $update['noble_time'] = $data['noble_time'];

        $str = $data['noble_name'].'开通成功,本次购买赠送了'.$data['price'].'点经验和'.$give.'钻石';

        $data  = self::sendUsrMsg('system',$data['user_id'],$str);

        //更新贵族信息
        Db::name('user_extend')->where('user_id',$dehash)->update($update);
        return true;
    }


    public static $errorMsg ;

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 支付宝退款
     */
    public static function aliRefund(array $order)
    {
        try{
            $alipay = Pay::alipay(self::$alipay);
            $data =  $alipay->refund($order);
        }catch (\Exception $e){
            self::$errorMsg = $e->getMessage();
            return false;
        }

        if ($data['code'] == 10000  ){
//            if ($data['fund_change'] == 'N'){
//                self::$errorMsg = '支付宝退款失败';
//            }else{
            return true;
//            }
        }else{
            self::$errorMsg = $data['msg'];
        }
        return false;

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 微信退款
     */
    public static function weChatRefund(array $order)
    {
        try{
            $pay = Pay::wechat(self::$wechat);
            $data =  $pay->refund($order);
        }catch (\Exception $e){
//            dd($e->getMessage());
            self::$errorMsg = $e->getMessage();
            return false;
        }

        if ($data['return_code'] == 'SUCCESS' && $data['return_msg'] == 'ok'  ){
//            if ($data['fund_change'] == 'N'){
//                self::$errorMsg = '支付宝退款失败';
//            }else{
            return true;
//            }
        }else{
            self::$errorMsg = $data['return_msg'];
        }
        return false;
    }

    public function test1()
    {
        $verify = cache('wechat');
        dd($verify);
    }




}